home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / SETJMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  862 b   |  34 lines

  1. /* setjmp.c --- BIBLE pp. 93-94 */
  2. #include <stdio.h>
  3. #include <setjmp.h>
  4. static jmp_buf this_place;
  5. static void fake_error(void);
  6. main()
  7. {
  8.     int retval;
  9.     retval = setjmp(this_place);
  10.     if(retval == 0)
  11.     {
  12.         printf("First return from \"setjmp\"\n");
  13.     }
  14.     else
  15.     {
  16.         printf("Second return from \"setjmp\" induced by call "
  17.                     "to \"longjmp\"\n");
  18.                 /* Do processing that's otherwise skipped.
  19.                 For example,  error recovery. */
  20.         printf("There may be an error handler here.\n  "
  21.                         "We simply exit.\n");
  22.         exit(retval);
  23.      }
  24.         /* Somewhere else, in another function call longjmp */
  25.     printf("Everything seemed fine until suddenly...\n");
  26.     fake_error();
  27. }
  28.                 /* ---------------------------------- */
  29. static void fake_error(void)
  30. {
  31.     printf("Illegal instruction\n");
  32.     printf("--- longjmp called ---\n");
  33.     longjmp(this_place, 1);
  34. }